package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.*;
import com.heima.pojo.Node;
import com.heima.pojo.VendingMachine;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.VmDto;
import com.heima.pojo.dto.VmPageDto;
import com.heima.pojo.vo.*;
import com.heima.service.NodeService;
import com.heima.service.VmService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
@Slf4j
@RequiredArgsConstructor
public class VmServiceImpl implements VmService {


    private final VmMapper vmMapper;

    private final NodeMapper nodeMapper;

    private final RegionMapper regionMapper;

    private final BusinessMapper businessMapper;

    private final VmTypeMapper vmTypeMapper;

    private final NodeServiceImpl nodeServiceImpl;

    /**
     * 分页查询设备
     *
     * @param dto
     * @return
     */
    @Override
    public VmPageVo pageQuery(VmPageDto dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<VendingMachineVo> page = vmMapper.pageQuery(dto);
        page = addData(page);
        VmPageVo vmPageVo = new VmPageVo();
        vmPageVo.setPageIndex(dto.getPageIndex());
        vmPageVo.setPageSize(dto.getPageSize());
        vmPageVo.setTotalPage(page.getPages());
        vmPageVo.setTotalCount(page.getTotal());
        vmPageVo.setCurrentPageRecords(page);
        return vmPageVo;
    }

    /**
     * 新增设备
     *
     * @param dto
     */
    @Override
    public void saveVm(VmDto dto) {
        VendingMachine vendingMachine = new VendingMachine();
        BeanUtils.copyProperties(dto, vendingMachine);
        if (!StringUtils.hasText(dto.getId())) {
            vendingMachine.setCreateUserId("1");
            vendingMachine.setCreateTime(LocalDateTime.now());
            vendingMachine.setUpdateTime(LocalDateTime.now());
            vendingMachine.setInnerCode(new Random().nextInt(100000, 9999999) + "");
            vmMapper.addVm(vendingMachine);
            return;
        }
        vmMapper.updateNodeId(vendingMachine);
    }


    /**
     * 此方法用于往vm中添加数据
     */
    public Page<VendingMachineVo> addData(Page<VendingMachineVo> machineVos) {
        log.info("开始封装数据");
        // 查询所有的node
        List<Node> nodes = nodeMapper.queryNode();
        List<NodeVo> nodeVos = nodes.stream().map(node -> {
            NodeVo nodeVo = new NodeVo();
            BeanUtils.copyProperties(node, nodeVo);
            return nodeVo;
        }).toList();
        nodeVos.forEach(nodeVo -> {
            // 从nodeVo中取出regionId并查询对应的regionVo
            RegionVo regionVo = regionMapper.queryRegion(nodeVo.getRegionId().toString());
            // 往regionVo中的nodeList中添加数据
            RegionVo doneRegionVo = nodeServiceImpl.addData(regionVo);
            // 将doneRegionVo封装进nodeVo
            nodeVo.setRegion(doneRegionVo);
            // 将businessType封装进nodeVo
            List<BusinessTypeVo> businessTypeVos = businessMapper.queryAllBusiness();
            businessTypeVos.forEach(businessTypeVo -> {
                if (businessTypeVo.getId().toString().equals(nodeVo.getBusinessId().toString())) {
                    nodeVo.setBusinessType(businessTypeVo);
                }
            });
            // 查询所有的vmType
            List<VmType> vmTypes = vmTypeMapper.queryType();
            machineVos.forEach(machineVo -> {
                if (machineVo.getNodeId().equals(nodeVo.getId().toString())) {
                    machineVo.setNode(nodeVo);
                }
                vmTypes.forEach(vmType -> {
                    if (Integer.parseInt(vmType.getTypeId()) == machineVo.getVmType()) {
                        machineVo.setType(vmType);
                    }
                });
                machineVo.setRegion(nodeVo.getRegion());
            });
        });
        // 查询所有的businessType
        List<BusinessTypeVo> businessTypeVos = businessMapper.queryAllBusiness();
        machineVos.forEach(machineVo -> {
            BusinessTypeVo businessTypeVo = businessTypeVos.stream().filter(e -> e.getId().toString().equals(machineVo.getBusinessId())).findFirst().orElse(null);
            if (businessTypeVo != null) {
                machineVo.setBusinessType(businessTypeVo);
            }
        });
        machineVos.forEach(machineVo -> {
            machineVo.setFault(false);
            Map<Integer, Boolean> map = new HashMap<>();
            map.put(10001, false);
            map.put(10002, false);
            map.put(10003, false);
            machineVo.setStatus(map);
        });
        log.info("封装数据结束");
        return machineVos;
    }
}
